<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css" />
    <link rel="stylesheet" href="css/reset.css" />
    <link rel="stylesheet" href="css/iconfont.css" />
    <link rel="stylesheet" href="css/main.css" />
    <!-- jq -->
    <script src="./libs/jquery-1.12.4.min.js"></script>
    <!-- 模板引擎 -->
    <script src="./libs/template-web.js"></script>
    <!-- 模态框 -->
    <script src="./libs/bootstrap/js/bootstrap.min.js"></script>
    <!-- 日期css文件 -->
    <link rel="stylesheet" href="./libs/jedate/css/jeDate-test.css" />
    <link rel="stylesheet" href="./libs/jedate/css/jedate.css" />
  </head>

  <body>
    <div class="container-fluid">
      <div class="common_title">
        文章发表
      </div>
      <div class="container-fluid common_con">
        <form class="form-horizontal article_form" id="form">
          <div class="form-group">
            <label for="inputTitle" class="col-sm-2 control-label"
              >文章标题：</label
            >
            <div class="col-sm-10">
              <input
                type="text"
                name="title"
                class="form-control title"
                id="inputTitle"
                value="文章标题文字"
              />
            </div>
          </div>
          <div class="form-group">
            <label for="inputCover" class="col-sm-2 control-label"
              >文章封面：</label
            >
            <div class="col-sm-10">
              <img src="images/pic06.jpg" class="article_cover" />
              <input name="cover" type="file" id="inputCover" />
            </div>
          </div>
          <div class="form-group">
            <label for="inputCategory" class="col-sm-2 control-label"
              >文章类别：</label
            >
            <div class="col-sm-4">
              <select class="form-control category" name="categoryId">
                <option>类别一</option>
                <option>类别二</option>
                <option>类别三</option>
                <option selected>类别四</option>
                <option>类别五</option>
              </select>
            </div>
          </div>
          <div class="form-group">
            <label class="col-sm-2 control-label">发布时间：</label>
            <div class="col-sm-4">
              <!-- 2.2 日期结构 -->
              <div class="jeinpbox">
                <input
                  type="text"
                  class="jeinput"
                  id="testico"
                  name="date"
                  placeholder="YYYY-MM-DD"
                />
                <div class="icons jebtns"></div>
              </div>
            </div>
          </div>
          <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label"
              >文章内容：</label
            >
            <div class="col-sm-10">
              <!-- 4.1 富文本插件结构 -->
              <div id="editor">
                <p>欢迎使用 <b>k-on</b> 富文本编辑器</p>
              </div>
            </div>
          </div>
          <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
              <button type="submit" class="btn btn-success btn-release">
                发布
              </button>
              <button type="submit" class="btn btn-default btn-draft">
                存为草稿
              </button>
            </div>
          </div>
        </form>
      </div>
    </div>
    <!-- 6.1 引入模态框结构 -->
    <div class="modal fade" tabindex="-1" role="dialog" id="myModal">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button
              type="button"
              class="close"
              data-dismiss="modal"
              aria-label="Close"
            >
              <span aria-hidden="true">&times;</span>
            </button>
            <h4 class="modal-title">温馨提示</h4>
          </div>
          <div class="modal-body">
            <p>One fine body&hellip;</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">
              确认
            </button>
          </div>
        </div>
        <!-- /.modal-content -->
      </div>
      <!-- /.modal-dialog -->
    </div>
    <!-- /.modal -->
  </body>
</html>

<!-- 1. 引入地址js文件 -->
<script src="./libs/https.js"></script>
<!-- 2. 引入日期js文件 -->
<script src="./libs/jedate/js/jedate.js"></script>
<!-- 4. 富文本js文件 -->
<script src="./libs/wangEditor/wangEditor.min.js"></script>

<!-- 3.1 模板引擎结构 -->
<script type="text/html" id="tpl-list">
  {{each}}
  <option value={{$index}}>{{$value.name}}</option>
  {{/each}}
</script>

<!-- 4. 调用富文本插件 -->
<script type="text/javascript">
  const E = window.wangEditor
  const editor = new E('#editor')
  editor.create()
</script>

<script>
  $(function () {
    // 2.4 调用日期插件
    $('.jebtns').on('click', function () {
      jeDate('#testico', {
        trigger: false,
        format: 'YYYY-MM-DD',
        theme: { bgcolor: '#00A1CB', color: '#ffffff', pnColor: '#00CCFF' },

        // 4.3 提高层级,避免与富文本框重叠
        zIndex: 20000
      })
    })

    // 3. 文章类别获取  渲染
    $.ajax({
      type: 'get',
      url: BigNew.category_list,
      success: function (backData) {
        // console.log(backData)
        if (backData.code == 200) {
          let data = backData.data
          let htmlStr = template('tpl-list', data)
          $('.category').html(htmlStr)
        }
      }
    })

    // 6. 监控图片修改按钮
    $('#inputCover').on('change', function (e) {
      e.preventDefault()
      let picFile = this.files[0]
      let picUrl = URL.createObjectURL(picFile)
      $('.article_cover').attr('src', picUrl)
    })
  })

  // 7. 给修改，存为草稿同时添加点击事件
  $('.btn-release, .btn-draft').on('click', function (e) {
    e.preventDefault()
    // 7.1 判断为修改还是存为草稿
    let state = ''
    if (
      $(this)
        .text()
        .trim() == '发布'
    ) {
      state = '已发布'
    } else {
      state = '草稿'
    }

    // 7.2 获取所有请求参数
    let fd = new FormData(document.querySelector('form'))
    // fd.append('id', id)
    fd.append('state', state)
    fd.append('content', editor.txt.text())
    // console.log(fd.get('id'))
    console.log(fd.get('title'))
    console.log(fd.get('cover'))
    console.log(fd.get('categoryId'))
    console.log(fd.get('date'))
    console.log(fd.get('content'))
    console.log(fd.get('state'))

    // 7.3 发送ajax请求
    $.ajax({
      type: 'post',
      url: BigNew.article_publish,
      data: fd,
      contentType: false,
      processData: false,
      success: function (backData) {
        console.log(backData)
        if (backData.code == 200) {
          $('#myModal').modal()
          $('.modal-body p').text(backData.msg)
          $('#myModal').on('hidden.bs.modal', function () {
            window.location = './article_list.html'
          })
        } else {
          $('#myModal').modle()
          $('.modal-body p').text(backData.msg)
        }
      }
    })
  })
</script>
